package com.data.csv;

import java.io.File;
import java.sql.Timestamp;
import java.util.*;

import com.base.bean.*;
import com.base.domain.ClientDetail;
import com.base.service.client.AdvanceClientService;
import com.data.csv.bean.ClientCSV;
/**
 * 导出某些人的客户(手动去删除某些列)
 * 	35	张明德
	44	张军莉
	73	周档墙
	27 勾文武

 * @author chz
 *
 */
public class ClientDeal {

	private AdvanceClientService acs;
	
	public ClientDeal(){
		acs =  AdvanceClientService.getInstance();
	}
	
	
	public static void main(String[] args){
		ClientDeal gd = new ClientDeal();
		gd.writeCSV("/Users/chz/Desktop/kh_27.csv",27,"勾文武");	//某人拥有客户的数据
	}
	
	
	
	public List<ClientCSV> getClientListByOwners(int uid,String name){
		List<ClientDetail> cds = new ArrayList<ClientDetail>();
		
		List<ClientBase> cbs = acs.getCbd().findByHql("from ClientBase cb where cb.state=1  and cb.owner="+uid);
		for(ClientBase cb:cbs){
			
			List<ClientPhone> cps = acs.getCpd().findByHql("from ClientPhone cp where cp.state=1 and cp.cid="+cb.getCid());
			ClientDetail cd = new ClientDetail();
			cd.setClientBase(cb);
			cd.setClientPhones(cps);
			cds.add(cd);
		}
		
		/*
		private Integer cid;
		private String name,comName,province,city,area,phone;
		private Timestamp created,modified;
		*/
		List<ClientCSV> list =  new ArrayList<ClientCSV>();
		for(ClientDetail var:cds){
			ClientCSV bean = new ClientCSV();
			bean.setCid(var.getClientBase().getCid());
			bean.setName(var.getClientBase().getName());
			bean.setComName(var.getClientBase().getComName());
			bean.setProvince(var.getClientBase().getProvince());
			bean.setCity(var.getClientBase().getCity());
			bean.setArea(var.getClientBase().getArea());
			bean.setCreated(var.getClientBase().getCreated());
			bean.setOwnname(name);
			List<ClientPhone> phones = var.getClientPhones();
			String str_phones = "";
			for(ClientPhone _cp:phones){
				str_phones=str_phones+_cp.getPhone()+"#";
			}
			
			
			bean.setPhone(str_phones);
			bean.setModified(var.getClientBase().getModified());
			list.add(bean);
		}
		
		
		
		return list;
	}
	
	public void writeCSV(String filePath,int uid,String name){
		
		List<String> list =  new ArrayList<String>();
		list.add(ClientCSV.getTitle());
		List<ClientCSV> dataList = this.getClientListByOwners(uid,name);
		for(ClientCSV data:dataList){
			list.add(data.toString());
		}
		
		tool.CSVUtils.exportCsv(new File(filePath), list);
	}
	
	
	
}
